/*
 ***************************************************************************
 *           Copyright © 2009 by konglinglong                              *
 *           konglinglong@gmail.com                                        *
 *           我的主页:https://sites.google.com/site/konglinglong/           *
 ***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************
 */

#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H

#include <QObject>
#include <QHostAddress>
#include <QString>
#include "Longdefines.h"


class QUdpSocket;
class NetworkManager:public QObject
{
  Q_OBJECT
public:
	//
	NetworkManager(QObject * parent = NULL);
	//
	~NetworkManager();
	//发送数据
	void send(const uint8 *outStr, int len);
	//
	void startConnect();
	//
	void stopConnect();
	//
	bool DNS();

signals:
	//这个信号与LongController的onHaveGotMsg(char *, int)槽相连，收到信息后，就会发送
	void gotMsg(char *inStr, int size);
	//当NetworkManager完成初始化后便会发送这个信号来通知LongController
	void networkManagerReady();

public slots:
	//设置服务器IP地址
	void setServerAddress(QHostAddress addr);
	//服务器IP地址设置完成后，就后调用这个槽进行初始化
	bool isNetworkReady();
	//收到应答信息后执行这个槽
	void processPendingDatagrams();

private:
	//在整个程序的运行过程中，都用这个套接字来发送接收包
	QUdpSocket * udpSocket;
	//服务器IP地址
	QHostAddress serverAddress;
	//网络是否准备好
	bool networkReady;
	//是否已成功解释域名
	bool dnsOK;
};

#endif //NETWORKMANAGER_H
